package com.sz.common.core.system.dto;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.sz.common.core.system.entity.Menu;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Function: MenuItem <br>
 * Author: Charles <br>
 * Date: 2016-10-28 15:25:00
 */
public class MenuItem {

    private Integer id;     //菜单id
    private String name;    //菜单名字
    private String url;     //菜单url
    private int parentId;       //菜单父节点
    private int ordinal;          //菜单的顺序
    private boolean isHidden;   //隐藏
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private Boolean selected;
    List<PermissionDto> permissions = new ArrayList<>();
    private List<MenuItem> children = new ArrayList<>();//菜单中包含的子节点

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public int getParentId() {
        return parentId;
    }

    public void setParentId(int parentId) {
        this.parentId = parentId;
    }

    public int getOrdinal() {
        return ordinal;
    }

    public void setOrdinal(int ordinal) {
        this.ordinal = ordinal;
    }

    @JsonIgnore
    public boolean isHidden() {
        return isHidden;
    }

    public void setIsHidden(boolean hidden) {
        isHidden = hidden;
    }

    public Boolean getSelected() {
        return selected;
    }

    public void setSelected(Boolean selected) {
        this.selected = selected;
    }

    public List<MenuItem> getChildren() {
        return children;
    }

    public boolean hasChild() {
        return this.children != null && this.children.size() > 0;
    }

    public MenuItem(int id, String name, String url, int parentId, int order) {
        this.id = id;
        this.name = name;
        this.url = url;
        this.parentId = parentId;
        this.ordinal = order;
    }

    public MenuItem() {
    }

    public MenuItem(Menu menu) {
        this.id = menu.getId();
        this.name = menu.getName();
        this.url = menu.getUrl();
        this.parentId = menu.getParentId();
        this.ordinal = menu.getOrdinal();
        this.isHidden = menu.getIsHidden();
    }

    public void addChild(MenuItem child) {
        this.children.add(child);
    }

    public List<PermissionDto> getPermissions() {
        return permissions;
    }

    public void setPermissions(List<PermissionDto> permissions) {
        this.permissions = permissions;
    }

    public void sort() {
        Collections.sort(this.children, new Comparator<MenuItem>() {
            @Override
            public int compare(MenuItem o1, MenuItem o2) {
                return (o1.getOrdinal() > o2.getOrdinal()) ? 1 : -1;
            }
        });
        for (MenuItem item : children) {
            item.sort();
        }
    }

}
